package net.thelostlands.plugins.thelostfortress;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
import com.palmergames.bukkit.towny.NotRegisteredException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

/**
 *
 * @author fabio
 */
public class Area implements Comparable {

    final static String NONAPERTA = "Non Aperta";
    int x1;
    int x2;
    int y1;
    int y2;
    int z1;
    int z2;
    HashMap<Location, String> chests;
    File file;
    YamlConfiguration conf;
    boolean deleted;

    public Area(int x1, int x2, int y1, int y2, int z1, int z2) {
        this.x1 = x1 < x2 ? x1 : x2;
        this.x2 = x1 < x2 ? x2 : x1;
        this.y1 = y1 < y2 ? y1 : y2;
        this.y2 = y1 < y2 ? y2 : y1;
        this.z1 = z1 < z2 ? z1 : z2;
        this.z2 = z1 < z2 ? z2 : z1;
        chests = new HashMap<Location, String>();
        file = new File(Main.dataFile + File.separator + x1 + x2 + y1 + y2 + z1 + z2 + ".yml");
        conf = new YamlConfiguration();
        conf.set("x1", this.x1);
        conf.set("x2", this.x2);
        conf.set("y1", this.y1);
        conf.set("y2", this.y2);
        conf.set("z1", this.z1);
        conf.set("z2", this.z2);
        deleted=false;
        try {
            conf.save(file);
        } catch (IOException ex) {
            Main.log.info("[TheLostFortress] Salvataggio non riuscito");
        }
    }

    public String isFortressTaken() {
        if (chests == null || chests.isEmpty() || deleted) {
            return null;
        }
        for (String s1 : chests.values()) {
            for (String s2 : chests.values()) {
                if (!s1.equals(s2)) {
                    try {
                        if (!Main.towny.getTownyUniverse().getNation(s2).getAllies().contains(Main.towny.getTownyUniverse().getNation(s1))) {
                            return null;
                        }
                    } catch (NotRegisteredException ex) {
                        return null;
                    }
                }
            }
        }
        String s = chests.values().iterator().next();
        return (!s.equals(NONAPERTA) ? s : null);
    }

    public void addChest(Location l) {
        if (chests.size() >= Main.maxChest) {
            return;
        }
        chests.put(l, NONAPERTA);
        toStringConfig();

    }

    public void clear() {
        for (Location l : chests.keySet()) {
            chests.put(l, NONAPERTA);
        }
        toStringConfig();
    }

    public void openChest(Location l, String player) {
        String s = player;
        chests.put(l, s);

    }

    public void breakChest(Location l) {
        chests.remove(l);
        toStringConfig();
    }

    boolean findChest(Location location) {
        return chests.containsKey(location);
    }

    @Override
    public int compareTo(Object t) {
        Area a = (Area) t;
        return x1 < a.x1 ? -1 : (x1 > a.x1 ? 1 : (x2 < a.x2 ? -1 : (x2 > a.x2 ? 1 : (z1 < a.z1 ? -1 : 1))));
    }

    boolean uguale(int x1, int x2, int y1, int y2, int z1, int z2) {
        return this.x1 == x1 && this.x2 == x2 && this.y1 == y1 && this.y2 == y2 && this.z1 == z1 && this.z2 == z2;
    }

    public boolean isIn(int x, int y, int z) {
        return !deleted && (x1 <= x && x <= x2 && z1 <= z && z <= z2 && y1 <= y && y <= y2);
    }

    public boolean isIn(Location l) {
        return isIn(l.getBlockX(), l.getBlockY(), l.getBlockZ());
    }

    public boolean isCreeperIn(int x, int y, int z) {
        return !deleted &&  (x1 - 5 <= x && x <= x2 + 5 && z1 - 5 <= z && z <= z2 + 5 && y1 - 5 <= y && y <= y2 + 5);

    }

    public boolean isIn(Block b) {
        return isIn(b.getX(), b.getY(), b.getZ());
    }

    @Override
    public String toString() {
        if (x1 > x2) {
            return "";
        }
        return "" + x1 + " " + x2 + " " + y1 + " " + y2 + " " + z1 + " " + z2 + " ;" + System.getProperty("line.separator");
    }

    /*
     * void load(File file) { try { if (!file.exists()) { return; } Scanner s =
     * new Scanner(new java.io.FileInputStream(file)); x1 = s.nextInt(); x2 =
     * s.nextInt(); y1 = s.nextInt(); y2 = s.nextInt(); z1 = s.nextInt(); z2 =
     * s.nextInt(); } catch (FileNotFoundException ex) {
     * Logger.getLogger(Area.class.getName()).log(Level.SEVERE, null, ex); } }
     *
     * void store(File file) { try { FileOutputStream fw = new
     * FileOutputStream(file); fw.write(toString().getBytes()); fw.close(); }
     * catch (IOException ex) {
     * Logger.getLogger(Area.class.getName()).log(Level.SEVERE, null, ex); } }
     */
    public String toStringConfig() {
        YamlConfiguration y = new YamlConfiguration();
        y.set("x1", this.x1);
        y.set("x2", this.x2);
        y.set("y1", this.y1);
        y.set("y2", this.y2);
        y.set("z1", this.z1);
        y.set("z2", this.z2);
        y.set("deleted", deleted);
        int i = 1;     

        for (Location l : chests.keySet()) {
            y.set("chest.chest" + i + ".x", l.getBlockX());
            y.set("chest.chest" + i + ".y", l.getBlockY());
            y.set("chest.chest" + i + ".z", l.getBlockZ());
            y.set("chest.chest" + i + ".open", chests.get(l));
            
            i++;

        }
        try {
            y.save(file);
        } catch (IOException ex) {
            Main.log.info("[TheLostFortress] Salvataggio non riuscito");
        }
        return y.saveToString();


    }

    public Area(File f) throws IOException, InvalidConfigurationException {
        YamlConfiguration y = new YamlConfiguration();

        y.load(f);
        file = f;
        conf = y;
        x1 = y.getInt("x1");
        x2 = y.getInt("x2");
        y1 = y.getInt("y1");
        y2 = y.getInt("y2");
        z1 = y.getInt("z1");
        z2 = y.getInt("z2");
        deleted = y.getBoolean("deleted");
        
        chests = new HashMap<Location, String>();
        if(y.getConfigurationSection("chest")==null){
            return;
        }
        for (String s : y.getConfigurationSection("chest").getKeys(false)) {
            double x = y.getDouble(s + ".x");
            double yY = y.getDouble(s + ".y");
            double z = y.getDouble(s + ".z");
            String open = y.getString(s+".open");
            Location l = new Location(Main.one.getServer().getWorld(Main.mondo), x, yY, z);
            chests.put(l, open);
        }
       
        /*
         * List<String> rules = y.getList("chest"); Iterator<String> iter =
         * rules.iterator(); while (iter.hasNext()) {
         * Main.log.info(iter.next());
        }
         */

    }

    void remove() {
        deleted=true;
        toStringConfig();
        
    }
}
